<!doctype html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>






<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Mac,Linux,内网穿透,frp," />





  <link rel="alternate" href="/atom.xml" title="Bingo's Blog" type="application/atom+xml" />




  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.1" />






<meta name="description" content="前言最近要开始搞持续构建，已经买了一台黑苹果主机放在公司当服务器，接下来需要穿透内网，做端口映射。之前实现过 SSH 反向隧道穿透 NAT，但太简陋了，也很慢，就算用 autossh 还是很容易断，需要探索新的方案。">
<meta name="keywords" content="Mac,Linux,内网穿透,frp">
<meta property="og:type" content="article">
<meta property="og:title" content="端口映射内网穿透方案探索">
<meta property="og:url" content="https://bingozb.github.io/54.html">
<meta property="og:site_name" content="Bingo's Blog">
<meta property="og:description" content="前言最近要开始搞持续构建，已经买了一台黑苹果主机放在公司当服务器，接下来需要穿透内网，做端口映射。之前实现过 SSH 反向隧道穿透 NAT，但太简陋了，也很慢，就算用 autossh 还是很容易断，需要探索新的方案。">
<meta property="og:image" content="https://bingozb.github.io/images/blog/54/architecture.png">
<meta property="og:updated_time" content="2017-09-23T17:19:34.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="端口映射内网穿透方案探索">
<meta name="twitter:description" content="前言最近要开始搞持续构建，已经买了一台黑苹果主机放在公司当服务器，接下来需要穿透内网，做端口映射。之前实现过 SSH 反向隧道穿透 NAT，但太简陋了，也很慢，就算用 autossh 还是很容易断，需要探索新的方案。">
<meta name="twitter:image" content="https://bingozb.github.io/images/blog/54/architecture.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: 'undefined',
      author: '博主'
    },
    algolia: {
      applicationID: 'JE30EVH7NI',
      apiKey: '862bb3deb9a3166ed99de0be6527f59c',
      indexName: 'blog',
      hits: {"per_page":10},
      labels: {"input_placeholder":"输入关键词进行搜索","hits_empty":"找不到关于「 ${query} 」的文章","hits_stats":"共找到 ${hits} 篇文章，花了 ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://bingozb.github.io/54.html"/>





  <title>端口映射内网穿透方案探索 | Bingo's Blog</title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?c8a59b9753bce3ec58f81c89e2acce96";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>










  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Bingo's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">Stay Hungry, Stay Foolish.</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocapitalize="off" autocomplete="off" autocorrect="off"
             placeholder="搜索..." spellcheck="false"
             type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="https://bingozb.github.io/54.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Bingo">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Bingo's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                端口映射内网穿透方案探索
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-09-23T08:09:43+08:00">
                2017-09-23
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/战斗笔记/" itemprop="url" rel="index">
                    <span itemprop="name">战斗笔记</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
                <i class="fa fa-comment-o"></i>
              </span>
              
                <a href="/54.html#SOHUCS" itemprop="discussionUrl">
                  <span id="changyan_count_unit" class="post-comments-count hc-comment-count" data-xid="54.html" itemprop="commentsCount"></span>
                </a>
              
            
          

          
          
             <span id="/54.html" class="leancloud_visitors" data-flag-title="端口映射内网穿透方案探索">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数 </span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>最近要开始搞持续构建，已经买了一台黑苹果主机放在公司当服务器，接下来需要穿透内网，做端口映射。之前实现过 SSH 反向隧道穿透 NAT，但太简陋了，也很慢，就算用 autossh 还是很容易断，需要探索新的方案。</p>
<a id="more"></a>
<h2 id="花生壳"><a href="#花生壳" class="headerlink" title="花生壳"></a>花生壳</h2><p>官网 <a href="http://www.oray.com" target="_blank" rel="external">http://www.oray.com</a></p>
<p>网上很多人说的都是说花生壳的内网穿透，这也从侧面说明花生壳的内网穿透使用起来应该是比较容易的。但这个实在没意思，傻瓜式操作。重点是，花生壳已经收费了，每个月还限制流量1G。对于网站来说，1个G能干什么，卖萌么。</p>
<p>如果对于穿透原理不是很执着，又不想操这个心自己折腾，花生壳内网版会是个很不错的选择。</p>
<h2 id="n2n"><a href="#n2n" class="headerlink" title="n2n"></a>n2n</h2><p>Github: </p>
<ul>
<li><a href="https://github.com/ntop/n2n" target="_blank" rel="external">https://github.com/ntop/n2n</a></li>
<li><a href="https://github.com/meyerd/n2n" target="_blank" rel="external">https://github.com/meyerd/n2n</a> (development branch)</li>
</ul>
<p>n2n 是在数据链路层实现的一套 P2P 协议，包括 super node 和 edge node。它是开源的，遵循 GPL v3 协议，可运行于 Linux，Windows，Android，甚至是 Openwrt 之上。</p>
<p>我在 CentOS 上编译完 N2N，创建了 super node。但在 MacOS 上编译失败了，遇到了一个 openssl 的报错：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div></pre></td><td class="code"><pre><div class="line">$ make</div><div class="line">Scanning dependencies of target scm</div><div class="line">[  3%] Building C object CMakeFiles/scm.dir/unix-scm.c.o</div><div class="line">[  6%] Linking C static library libscm.a</div><div class="line">[  6%] Built target scm</div><div class="line">Scanning dependencies of target doc</div><div class="line">[ 10%] Generating doc/edge.8.gz</div><div class="line">[ 13%] Generating doc/supernode.1.gz</div><div class="line">[ 17%] Generating doc/n2n_v2.7.gz</div><div class="line">[ 17%] Built target doc</div><div class="line">Scanning dependencies of target n2n</div><div class="line">[ 20%] Building C object CMakeFiles/n2n.dir/n2n.c.o</div><div class="line">/Users/bingo/Desktop/n2n/n2n_v2/n2n.c:458:65: warning: format specifies type &apos;unsigned short&apos; but the argument has type &apos;int&apos; [-Wformat]</div><div class="line">        snprintf( out, N2N_SOCKBUF_SIZE, &quot;%hu.%hu.%hu.%hu:%hu&quot;, (a[0] &amp; 0xff),</div><div class="line">                                          ~~~                   ^~~~~~~~~~~~~</div><div class="line">                                          %d</div><div class="line">/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include/secure/_stdio.h:57:62: note: expanded from macro</div><div class="line">      &apos;snprintf&apos;</div><div class="line">  __builtin___snprintf_chk (str, len, 0, __darwin_obsz(str), __VA_ARGS__)</div><div class="line">                                                             ^~~~~~~~~~~</div><div class="line">/Users/bingo/Desktop/n2n/n2n_v2/n2n.c:459:17: warning: format specifies type &apos;unsigned short&apos; but the argument has type &apos;int&apos; [-Wformat]</div><div class="line">                (a[1] &amp; 0xff), (a[2] &amp; 0xff), (a[3] &amp; 0xff), sock-&gt;port );</div><div class="line">                ^~~~~~~~~~~~~</div><div class="line">/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include/secure/_stdio.h:57:62: note: expanded from macro</div><div class="line">      &apos;snprintf&apos;</div><div class="line">  __builtin___snprintf_chk (str, len, 0, __darwin_obsz(str), __VA_ARGS__)</div><div class="line">                                                             ^~~~~~~~~~~</div><div class="line">/Users/bingo/Desktop/n2n/n2n_v2/n2n.c:459:32: warning: format specifies type &apos;unsigned short&apos; but the argument has type &apos;int&apos; [-Wformat]</div><div class="line">                (a[1] &amp; 0xff), (a[2] &amp; 0xff), (a[3] &amp; 0xff), sock-&gt;port );</div><div class="line">                               ^~~~~~~~~~~~~</div><div class="line">/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include/secure/_stdio.h:57:62: note: expanded from macro</div><div class="line">      &apos;snprintf&apos;</div><div class="line">  __builtin___snprintf_chk (str, len, 0, __darwin_obsz(str), __VA_ARGS__)</div><div class="line">                                                             ^~~~~~~~~~~</div><div class="line">/Users/bingo/Desktop/n2n/n2n_v2/n2n.c:459:47: warning: format specifies type &apos;unsigned short&apos; but the argument has type &apos;int&apos; [-Wformat]</div><div class="line">                (a[1] &amp; 0xff), (a[2] &amp; 0xff), (a[3] &amp; 0xff), sock-&gt;port );</div><div class="line">                                              ^~~~~~~~~~~~~</div><div class="line">/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include/secure/_stdio.h:57:62: note: expanded from macro</div><div class="line">      &apos;snprintf&apos;</div><div class="line">  __builtin___snprintf_chk (str, len, 0, __darwin_obsz(str), __VA_ARGS__)</div><div class="line">                                                             ^~~~~~~~~~~</div><div class="line">4 warnings generated.</div><div class="line">[ 24%] Building C object CMakeFiles/n2n.dir/n2n_keyfile.c.o</div><div class="line">[ 27%] Building C object CMakeFiles/n2n.dir/wire.c.o</div><div class="line">[ 31%] Building C object CMakeFiles/n2n.dir/minilzo.c.o</div><div class="line">[ 34%] Building C object CMakeFiles/n2n.dir/twofish.c.o</div><div class="line">[ 37%] Building C object CMakeFiles/n2n.dir/transform_null.c.o</div><div class="line">[ 41%] Building C object CMakeFiles/n2n.dir/transform_tf.c.o</div><div class="line">[ 44%] Building C object CMakeFiles/n2n.dir/transform_aes.c.o</div><div class="line">/Users/bingo/Desktop/n2n/n2n_v2/transform_aes.c:12:10: fatal error: &apos;openssl/aes.h&apos; file not found</div><div class="line">#include &quot;openssl/aes.h&quot;</div><div class="line">         ^</div><div class="line">1 error generated.</div><div class="line">make[2]: *** [CMakeFiles/n2n.dir/transform_aes.c.o] Error 1</div><div class="line">make[1]: *** [CMakeFiles/n2n.dir/all] Error 2</div><div class="line">make: *** [all] Error 2</div></pre></td></tr></table></figure>
<p>即使我用 Homebrew 安装了最新版本的 OpenSSL，并且 link 让 shell 默认使用 Homebrew 安装的 OpenSSL，确定环境没问题后，依然还是报这个错误。</p>
<p>谷歌无果，查到的资料基本都是关于 iOS 使用 CocoaPods 安装 openssl 依赖的时候出现的错误。</p>
<p>很搞笑的是，百度用「n2n macOS」作为关键词，搜出来的结果居然只有两三条相关的，一条是 「有人在 OS X 下编译 n2n 成功的么? - V2EX」，还有一条是「有在osx系统下使用n2n vpn的没？」，而且时间是在2013和2014年，也就是三四年过去了，都没有人用了？</p>
<p>也许它只是不支持 MacOS 而已，不满足我的需求，我选择放弃 n2n。</p>
<h2 id="localtunnel"><a href="#localtunnel" class="headerlink" title="localtunnel"></a>localtunnel</h2><p>官网: <a href="https://localtunnel.github.io/www/" target="_blank" rel="external">https://localtunnel.github.io/www/</a><br>Github: <a href="https://github.com/localtunnel/localtunnel" target="_blank" rel="external">https://github.com/localtunnel/localtunnel</a></p>
<h3 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">$ npm install -g localtunnel</div><div class="line">/usr/local/bin/lt -&gt; /usr/local/lib/node_modules/localtunnel/bin/client</div><div class="line">+ localtunnel@1.8.3</div></pre></td></tr></table></figure>
<h3 id="使用"><a href="#使用" class="headerlink" title="使用"></a>使用</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">lt --port 8888</div><div class="line">your url is: https://nlatvemdpo.localtunnel.me</div></pre></td></tr></table></figure>
<h3 id="评论"><a href="#评论" class="headerlink" title="评论"></a>评论</h3><p>每次跑起来，域名主机名都是随机的，不能自定义域名，只支持 HTTP 协议的隧道，并且很慢，时不时502报错，直接淘汰。搞不懂 star 数为什么会有五千多，看来写 js 的群体人数真的不少啊，抱团点赞，维护 js 的生态圈？</p>
<h2 id="PageKite"><a href="#PageKite" class="headerlink" title="PageKite"></a>PageKite</h2><p>官网 <a href="http://pagekite.net/" target="_blank" rel="external">http://pagekite.net/</a></p>
<h3 id="安装-1"><a href="#安装-1" class="headerlink" title="安装"></a>安装</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div></pre></td><td class="code"><pre><div class="line">$ curl -s https://pagekite.net/pk/ |sudo bash</div><div class="line">+ curl https://pagekite.net/pk/pagekite.py</div><div class="line">  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current</div><div class="line">                                 Dload  Upload   Total   Spent    Left  Speed</div><div class="line">100  169k  100  169k    0     0  60694      0  0:00:02  0:00:02 --:--:-- 60686</div><div class="line">+ chmod +x /usr/local/bin/pagekite.py</div><div class="line">+ &apos;[&apos; &apos;&apos; &apos;!=&apos; &apos;&apos; &apos;]&apos;</div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line">~~~~&apos;~~~~~,~~~~~~&apos;~~~~&lt;/&gt;</div><div class="line"> Welcome to PageKite!</div><div class="line"></div><div class="line">PageKite has been installed to /usr/local/bin/pagekite.py !</div><div class="line"></div><div class="line">Some useful commands:</div><div class="line"></div><div class="line">  $ pagekite.py --signup             # Sign up for service</div><div class="line">  $ pagekite.py 80 NAME.pagekite.me  # Expose port 80 as NAME.pagekite.me</div><div class="line"></div><div class="line">For further instructions:</div><div class="line"></div><div class="line">  $ pagekite.py --help |less</div></pre></td></tr></table></figure>
<h3 id="使用-1"><a href="#使用-1" class="headerlink" title="使用"></a>使用</h3><figure class="highlight stylus"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ pagekite<span class="selector-class">.py</span> <span class="number">8888</span> dankal<span class="selector-class">.pagekite</span><span class="selector-class">.me</span></div></pre></td></tr></table></figure>
<h3 id="评论-1"><a href="#评论-1" class="headerlink" title="评论"></a>评论</h3><p>稍微比较稳定，支持 HTTPS。去到官网后台管理才发现，原来是收费的！那我要你何用？各种限制，平均一个月也要二三十块，我还不如用花生壳呢。</p>
<h2 id="ngrok"><a href="#ngrok" class="headerlink" title="ngrok"></a>ngrok</h2><p>官网 <a href="https://www.ngrok.cc" target="_blank" rel="external">https://www.ngrok.cc</a><br>控制台 <a href="https://www.ngrok.cc/user.html" target="_blank" rel="external">https://www.ngrok.cc/user.html</a></p>
<p>有免费版和收费版</p>
<table>
<thead>
<tr>
<th></th>
<th>免费版</th>
<th>收费版</th>
</tr>
</thead>
<tbody>
<tr>
<td>服务器</td>
<td>香港免费服务器</td>
<td>香港200M VIP服务器</td>
</tr>
<tr>
<td>内存</td>
<td>16G</td>
<td>16G</td>
</tr>
<tr>
<td>硬盘</td>
<td>32G</td>
<td>32G</td>
</tr>
<tr>
<td>地区</td>
<td>香港</td>
<td>香港</td>
</tr>
<tr>
<td>带宽</td>
<td>10M</td>
<td>200M</td>
</tr>
</tbody>
</table>
<h3 id="安装-2"><a href="#安装-2" class="headerlink" title="安装"></a>安装</h3><p>客户端下载 <a href="https://www.ngrok.cc/#down-client" target="_blank" rel="external">https://www.ngrok.cc/#down-client</a>，选择对应的平台下载。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"># Mac OSX 64Bit版本</div><div class="line">$ wget http://hls.ctopus.com/sunny/darwin_amd64.zip</div></pre></td></tr></table></figure>
<h3 id="使用-2"><a href="#使用-2" class="headerlink" title="使用"></a>使用</h3><p>需要先在控制台新建隧道</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">隧道域名:	<span class="selector-tag">dankal</span></div><div class="line">自定义域名: <span class="selector-tag">build-app</span><span class="selector-class">.dankal</span><span class="selector-class">.cn</span></div><div class="line">隧道端口:       <span class="selector-tag">-</span></div><div class="line"><span class="selector-tag">http</span>验证用户名: <span class="selector-tag">-</span></div><div class="line"><span class="selector-tag">http</span>验证密码:   <span class="selector-tag">-</span></div><div class="line">开通日期:	2017<span class="selector-tag">-09-09</span> 14<span class="selector-pseudo">:56</span><span class="selector-pseudo">:12</span></div><div class="line">服务器地址: <span class="selector-tag">server</span><span class="selector-class">.ngrok</span><span class="selector-class">.cc</span>（路由器链接的时候填写，请不要暴露此地址，避免服务器遭受攻击，谢谢）</div></pre></td></tr></table></figure>
<p>拿到对应隧道的 ID 后，使用客户端目录下的 sunny 脚本执行：</p>
<figure class="highlight x86asm"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">$ ./sunny clientid e807a9ffdd555f09</div><div class="line">Sunny-Ngrok   官 网 www.ngrok.cc                      (Ctrl+C 退 出 )</div><div class="line"></div><div class="line">隧 道 状 态                        在 线</div><div class="line">版 本                             <span class="number">2.1</span>/<span class="number">2.1</span></div><div class="line">转 发                             http://build-app.dankal.cn -&gt; <span class="number">127.0</span><span class="meta">.0</span><span class="meta">.1</span>:<span class="number">8888</span></div><div class="line">Web界 面                          <span class="number">127.0</span><span class="meta">.0</span><span class="meta">.1</span>:<span class="number">4040</span></div><div class="line"># Conn                           <span class="number">36</span></div><div class="line">Avg Conn Time                    <span class="number">22906.</span>71ms</div></pre></td></tr></table></figure>
<h3 id="评论-2"><a href="#评论-2" class="headerlink" title="评论"></a>评论</h3><p>很稳定，支持自定义域名，但自定义域名会比前缀域名速度慢很多，暂时不支持 https。免费版勉强可以用，但是带宽感觉会不够用。收费版的话，又不想花钱。</p>
<p>能不能自己搭建？答案是肯定的。但是，ngrok 1.x 已经不再开源，而 2.x 已经商业化不开源。而且 1.x 有 bug，比如内存泄漏，其功能也较少。所以，评估过后，我决定不踩进去了，直接跳过，继续选择其它方案。</p>
<h2 id="frp"><a href="#frp" class="headerlink" title="frp"></a>frp</h2><p>frp 是一个高性能的反向代理应用，可以轻松地进行内网穿透，对外网提供服务，支持 TCP、UDP、HTTP、HTTPS 等协议类型，并且 web 服务支持根据域名进行路由转发。</p>
<p><img src="/images/blog/54/architecture.png" alt="frp"></p>
<p>Github: <a href="https://github.com/fatedier/frp" target="_blank" rel="external">https://github.com/fatedier/frp</a><br>Releases: <a href="https://github.com/fatedier/frp/releases" target="_blank" rel="external">https://github.com/fatedier/frp/releases</a></p>
<h3 id="安装-3"><a href="#安装-3" class="headerlink" title="安装"></a>安装</h3><p>可以直接下载编译好的压缩包，在上面的 Releases 页面中找到对应平台的压缩包，解压后就可以直接使用，或者自己用 GO 语言编译。</p>
<h3 id="使用-3"><a href="#使用-3" class="headerlink" title="使用"></a>使用</h3><p>关于 frp 的配置，我会再写一篇文章详细介绍。这里先略过 ini 文件的配置，直接启动 frp 服务。</p>
<h4 id="服务端"><a href="#服务端" class="headerlink" title="服务端"></a>服务端</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ ./frps -c frps.ini</div></pre></td></tr></table></figure>
<h4 id="客户端"><a href="#客户端" class="headerlink" title="客户端"></a>客户端</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ ./frpc -c frpc.ini</div></pre></td></tr></table></figure>
<h3 id="评论-3"><a href="#评论-3" class="headerlink" title="评论"></a>评论</h3><p>frp 是我目前发现最合适的端口映射方案，免费，稳定。配置灵活方便，支持的功能也很多，这正是我想要的！</p>
<h2 id="后话"><a href="#后话" class="headerlink" title="后话"></a>后话</h2><p>经过一段时间的摸索和评估，最终我采用了 frp 来做端口映射穿透内网。无论是公司的黑苹果主机服务器，还是我的树莓派服务器，都会使用 frp 来做端口映射。</p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        
  <ul class="post-copyright">
    <li class="post-copyright-author">
      <strong>本文作者：</strong>
      Bingo
    </li>
    <li class="post-copyright-link">
      <strong>本文链接：</strong>
      <a href="https://bingozb.github.io/54.html" title="端口映射内网穿透方案探索">https://bingozb.github.io/54.html</a>
    </li>
    <li class="post-copyright-license">
      <strong>版权声明： </strong>
      本博客所有文章除特别声明外，均采用 MIT 许可协议。转载请注明出处！
    </li>
  </ul>


      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Mac/" rel="tag"># Mac</a>
          
            <a href="/tags/Linux/" rel="tag"># Linux</a>
          
            <a href="/tags/内网穿透/" rel="tag"># 内网穿透</a>
          
            <a href="/tags/frp/" rel="tag"># frp</a>
          
        </div>
      

      
        
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/55.html" rel="next" title="frp端口映射穿透内网">
                <i class="fa fa-chevron-left"></i> frp端口映射穿透内网
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/53.html" rel="prev" title="MacOS使用Homebrew安装的OpenSSL">
                MacOS使用Homebrew安装的OpenSSL <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
      <div id="SOHUCS"></div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/avatar.jpg"
               alt="Bingo" />
          <p class="site-author-name" itemprop="name">Bingo</p>
           
              <p class="site-description motion-element" itemprop="description">He who hesitates is lost.</p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/archives">
                <span class="site-state-item-count">55</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-categories">
              <a href="/categories/index.html">
                <span class="site-state-item-count">4</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">22</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        
          <div class="feed-link motion-element">
            <a href="/atom.xml" rel="alternate">
              <i class="fa fa-rss"></i>
              RSS
            </a>
          </div>
        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/bingozb" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://coding.net/u/bingozb" target="_blank" title="Coding">
                  
                    <i class="fa fa-fw fa-git"></i>
                  
                  Coding
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://twitter.com/bingo_v587" target="_blank" title="Twitter">
                  
                    <i class="fa fa-fw fa-twitter"></i>
                  
                  Twitter
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://www.facebook.com/profile.php?id=100013406670597" target="_blank" title="Facebook">
                  
                    <i class="fa fa-fw fa-facebook"></i>
                  
                  Facebook
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#前言"><span class="nav-number">1.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#花生壳"><span class="nav-number">2.</span> <span class="nav-text">花生壳</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#n2n"><span class="nav-number">3.</span> <span class="nav-text">n2n</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#localtunnel"><span class="nav-number">4.</span> <span class="nav-text">localtunnel</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#安装"><span class="nav-number">4.1.</span> <span class="nav-text">安装</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#使用"><span class="nav-number">4.2.</span> <span class="nav-text">使用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#评论"><span class="nav-number">4.3.</span> <span class="nav-text">评论</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#PageKite"><span class="nav-number">5.</span> <span class="nav-text">PageKite</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#安装-1"><span class="nav-number">5.1.</span> <span class="nav-text">安装</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#使用-1"><span class="nav-number">5.2.</span> <span class="nav-text">使用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#评论-1"><span class="nav-number">5.3.</span> <span class="nav-text">评论</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#ngrok"><span class="nav-number">6.</span> <span class="nav-text">ngrok</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#安装-2"><span class="nav-number">6.1.</span> <span class="nav-text">安装</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#使用-2"><span class="nav-number">6.2.</span> <span class="nav-text">使用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#评论-2"><span class="nav-number">6.3.</span> <span class="nav-text">评论</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#frp"><span class="nav-number">7.</span> <span class="nav-text">frp</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#安装-3"><span class="nav-number">7.1.</span> <span class="nav-text">安装</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#使用-3"><span class="nav-number">7.2.</span> <span class="nav-text">使用</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#服务端"><span class="nav-number">7.2.1.</span> <span class="nav-text">服务端</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#客户端"><span class="nav-number">7.2.2.</span> <span class="nav-text">客户端</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#评论-3"><span class="nav-number">7.3.</span> <span class="nav-text">评论</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#后话"><span class="nav-number">8.</span> <span class="nav-text">后话</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  Copyright © 
  <span itemprop="copyrightYear">2017
  </span>
  <span class="author" itemprop="copyrightHolder">Bingo
  </span>
  All rights reserved.
</div>
        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.1"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.1"></script>



  


  




	





  





  





  




  
    <script type="text/javascript">
    (function(){
      var appid = 'cytcdzpa3';
      var conf = 'c0c6117021d1cea9f11629100efd8446';
      var width = window.innerWidth || document.documentElement.clientWidth;
      if (width < 960) {
      window.document.write('<script id="changyan_mobile_js" charset="utf-8" type="text/javascript" src="https://changyan.sohu.com/upload/mobile/wap-js/changyan_mobile.js?client_id=' + appid + '&conf=' + conf + '"><\/script>'); } else { var loadJs=function(d,a){var c=document.getElementsByTagName("head")[0]||document.head||document.documentElement;var b=document.createElement("script");b.setAttribute("type","text/javascript");b.setAttribute("charset","UTF-8");b.setAttribute("src",d);if(typeof a==="function"){if(window.attachEvent){b.onreadystatechange=function(){var e=b.readyState;if(e==="loaded"||e==="complete"){b.onreadystatechange=null;a()}}}else{b.onload=a}}c.appendChild(b)};loadJs("https://changyan.sohu.com/upload/changyan.js",function(){
        window.changyan.api.config({appid:appid,conf:conf})});
      }
    })();
    </script>
    <script type="text/javascript" src="https://assets.changyan.sohu.com/upload/plugins/plugins.count.js"></script>
  



  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (search_path.endsWith("json")) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      $('#local-search-input').focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('-1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.1.js"></script>
  <script>AV.initialize("A9MeTkIR49W9kOOSvkggsXge-gzGzoHsz", "HaIEOSDyxpcjwOjRjCYxiWjA");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  
<script>
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>


  

  

  

</body>
</html>
